package Interface;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import MonteCarlo.MonteCarlo;


public class Interface extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	JTextField fieldNThread;
	JMenuItem m1;
	JMenuItem m2;
	JMenuItem m3;
	JMenuItem m4;
	JMenuItem m5;
	
	JLabel fieldFlag;
	JLabel fieldS;
	JLabel fieldX; 
	JLabel fieldT;
	JLabel fieldr;
	JLabel fieldb;
	JLabel fieldv;
	JLabel fieldNSteps;
	JLabel fieldNSimulations;
	
	JButton start;
	JLabel result;
	JFrame popup;
	String setArguments;
	
	MonteCarlo mc;
	
	@Override
	protected void frameInit() {
		super.frameInit();
		setName("Monte Carlo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JMenuBar menuBar = new JMenuBar();
		JMenu m = new JMenu("Arguments");
		menuBar.add(m);
		m1 = new JMenuItem("Set 1");
		m1.addActionListener(this);
		m.add(m1);
		m2 = new JMenuItem("Set 2");
		m2.addActionListener(this);
		m.add(m2);
		m3 = new JMenuItem("Set 3");
		m3.addActionListener(this);
		m.add(m3);
		m4 = new JMenuItem("Set 4");
		m4.addActionListener(this);
		m.add(m4);
		m5 = new JMenuItem("Set 5");
		m5.addActionListener(this);
		m.add(m5);
		setJMenuBar(menuBar);
		getContentPane().setLayout(new GridLayout(11,2));
		JLabel flag = new JLabel("CallPutFlag");
		fieldFlag = new JLabel("");
		getContentPane().add(flag);
		getContentPane().add(fieldFlag);
		
		JLabel S = new JLabel("Prix actuel du sous-jacent S");
		fieldS = new JLabel("");
		getContentPane().add(S);
		getContentPane().add(fieldS);
		
		JLabel X = new JLabel("Prix d'exercice X");
		fieldX = new JLabel("");
		getContentPane().add(X);
		getContentPane().add(fieldX);
		
		JLabel T = new JLabel("Temps restant jusqu'à échéance");
		fieldT = new JLabel("");
		getContentPane().add(T);
		getContentPane().add(fieldT);
		
		JLabel r = new JLabel("Taux d'intérêt sans risque");
		fieldr = new JLabel("");
		getContentPane().add(r);
		getContentPane().add(fieldr);
		
		JLabel b = new JLabel("Taux des frais de détention");
		fieldb = new JLabel("");
		getContentPane().add(b);
		getContentPane().add(fieldb);
		
		JLabel v = new JLabel("Volatilité du prix sous-jacent");
		fieldv = new JLabel("");
		getContentPane().add(v);
		getContentPane().add(fieldv);
		
		JLabel nSteps = new JLabel("Nombre de points intermédiaires");
		fieldNSteps = new JLabel("");
		getContentPane().add(nSteps);
		getContentPane().add(fieldNSteps);
		
		JLabel nSimulations = new JLabel("Nombre de simulations à exécuter");
		fieldNSimulations = new JLabel("");
		getContentPane().add(nSimulations);
		getContentPane().add(fieldNSimulations);
		
		JLabel nThread = new JLabel("Nombre de threads à exécuter");
		getContentPane().add(nThread);
		fieldNThread = new JTextField(10);
		getContentPane().add(fieldNThread);
		
		start = new JButton();
		start.setText("Lancer");
		result = new JLabel();
		result.setText("");	
		getContentPane().add(start);
		getContentPane().add(result);
		start.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int nThread = Integer.parseInt(fieldNThread.getText());
			/*if(e.getSource() == m1)
			{
				setArguments = "config.txt";
				MonteCarlo mc = new MonteCarlo("setArguments");
				
			}*/
			if(e.getSource() == start)
			{
				mc = new MonteCarlo();
				mc.setStandardOption("config.txt", nThread);
	
				Double result = mc.compute();
				popup = new JFrame("Résultat");
				JLabel resultFrame = new JLabel();
				resultFrame.setText(result + " in " + mc.getTime() / 1000 + "."
					+ mc.getTime() % 1000 + " second(s).");
				popup.add(resultFrame);
				popup.setSize(400, 100);
	    		popup.setVisible(true);
			}
		
	}
	
		
}
